import { routing } from "../../utils/routing";

Page({
    redirectURL:'',
    data: {
        licNo: '',
        name: '',
        birthDate: '1999-09-09',
        hasLicImg: '',
        gendersIndex: null as null | number,
        genders: ['未知', '男', '女', '其他'],
        state: 'UNSUBMITTED' as 'UNSUBMITTED' | 'PENDING' | 'VERIFIED'
    },
    onLoad(opt: Record<'redirect', string>){
        const o: routing.RegisterOpts = opt
        if(o.redirect){
            this.redirectURL = decodeURIComponent(o.redirect)
        }
    },
    onUploadLic(){
        wx.chooseImage({
            success: res => {
                if( res.tempFilePaths.length <= 0 || res.tempFilePaths.length > 1){
                    wx.showToast({
                        icon: 'none',
                        title: '请选择一张图片'
                    })
                    return
                }
                setTimeout( () => {
                    this.setData({
                        hasLicImg: res.tempFilePaths[0],
                        name: '张三',
                        gendersIndex: 1,
                        birthDate: '1989-09-09',
                        licNo: '1155533333',
                        state: 'UNSUBMITTED',
                    })
                }, 1000)
                
            }
        })
    },
    changeGender(e: any){
        this.setData({
            gendersIndex: e.detail.value,
        })
    },
    changeBirthDate(e: any){
        this.setData({
            birthDate: e.detail.value,
        })
    },
    onSubmit(){
        this.setData({
            state: 'PENDING',
        })
        setTimeout( ()=>{
            this.onLicVerified()
        },3000)
        
    },
    onReSubmit(){
        this.setData({
            state: 'UNSUBMITTED',
            hasLicImg: '',
            name: '',
            gendersIndex: null,
            licNo: ''
        })
    },
    onLicVerified(){
        this.setData({
            state: 'VERIFIED'
        })
        if( this.redirectURL){
            wx.redirectTo({
                url: this.redirectURL
            })
        }
        
    }
})